/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.statemachine.editpart;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.tools.CellEditorLocator;
import org.eclipse.gef.tools.DirectEditManager;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.ElementImport;
import org.eclipse.uml2.uml.FinalState;

import com.cea.papyrus.core.directedit.ILabelControler;
import com.cea.papyrus.core.directedit.LabelCellEditorLocator;
import com.cea.papyrus.core.directedit.LabelControler;
import com.cea.papyrus.core.directedit.LabelDirectEditManager;
import com.cea.papyrus.core.directedit.PropertyAccessor;
import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.GraphNodeWithSemanticEditPart;
import com.cea.papyrus.core.editpart.connection.AnchorMngr;
import com.cea.papyrus.core.editpart.connection.EllipseAnchorMngr;
import com.cea.papyrus.core.figures.ElementFigure;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagram.statemachine.figure.FinalStateFigure;
import com.cea.papyrus.diagram.statemachine.figure.PseudostateFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

/**
 * Controler for FinalState in state machine diagrams.
 * 
 * @author David Servat
 * @since 29 jan. 07
 */
public class FinalStateEditPart extends GraphNodeWithSemanticEditPart {

	/**
	 * The channel.
	 */
	private int channel = 501;

	/**
	 * Manager for editing PrimitiveType name.
	 */
	private DirectEditManager manager;

	/**
	 * The constructor.
	 * 
	 * @param modelManager the associated ModelManager
	 * @param graphNode the GraphNode to be managed
	 */
	public FinalStateEditPart(GraphNode graphNode,ModelManager modelManager) {
		super(graphNode,modelManager);

		Debug.debug(this,"call FinalStateEditPart(GraphNode graphnode, ModelManager modelManager)",channel);
	}

	/**
	 * Calls for the appropriate figure associated to the element.
	 * 
	 * @return 
	 * 
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#createFigure()
	 */
	@Override
	protected ElementFigure getNodeFigure(){
		return (ElementFigure)createFigure() ;
	}

	/**
	 * Direct creation of a figure here based on the associated icon of the UML element.
	 * 
	 * @return 
	 * 
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#createFigure()
	 */
	@Override
	protected IFigure createFigure(){
		return new FinalStateFigure();
	}

	/**
	 * This method is called whenever graphical changes need to be performed.
	 * 
	 * @see org.eclipse.gef.editparts.AbstractEditPart#refreshVisuals()
	 */
	@Override
	protected void refreshVisuals() {
		Dimension size = getGraphNode().getSize();
		Point p = getGraphNode().getPosition();
		Rectangle r = new Rectangle(p ,size);

		IFigure fig = getFigure();
		((GraphicalEditPart)getParent()).setLayoutConstraint(this, fig, r);
	}

	/**
	 * Creates the anchor manager.
	 * 
	 * @return the anchor mngr
	 */
	@Override
	protected AnchorMngr createAnchorManager() {
		return new EllipseAnchorMngr(this);
	}

	/**
	 * Returns the associated UML element.
	 * 
	 * @return 
	 * 
	 * @see com.cea.papyrus.common.editpart.GraphNodeWithSemanticEditPart#getUmlElement()
	 */
	@Override
	public FinalState getUmlElement(){
		Element e = ((Uml1SemanticModelBridge)getGraphNode().getSemanticModel()).getElement();
        // Case of element import
        if(e instanceof ElementImport)
        	return (FinalState)((ElementImport)e).getImportedElement();
		return (FinalState)e;
	}

	/**
	 * Returns the Name Label of this edit part.
	 * 
	 * @return Label
	 */
	protected Label getNameLabel(){
		IFigure fig = getFigure();

		if(fig instanceof PseudostateFigure){
			return (Label)fig;
		} 
		else if(fig instanceof ElementFigure){
			return ((ElementFigure)fig).getNameLabel();
		} 
		else{
			Debug.debug(this, "FinalStateEditPart::getNameLabel() : Unknown Figure type.", channel);
			return null;
		}
	}
	
	/**
	 * {@inheritDoc} 
	 */
	@Override
	protected boolean checkDefaultEdition() {
		return false;
	}
}
